﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class MenuInRoleBO : BaseBO
    {
        #region Field list in database
        public const string FIELD_APP_NAME= "AppName";
        public const string FIELD_MENU_IN_ROLE_ID = "MenuInRoleID";
        public const string FIELD_MENU_ID = "MenuID";
        public const string FIELD_ROLE_ID = "RoleID";
        public const string FIELD_DATE_CREATED = "DateCreated";
        public const string FIELD_DATE_MODIFIED = "DateModified";
        public const string FIELD_IS_ACTIVE = "IsActive";
        public const string FIELD_DESCRIPTION = "Descriptions";
        public const string FIELD_ROLE_NAME = "RoleName";
        public const string FIELD_MENU_NAME = "MenuName";
        public const string FIELD_MENU_CODE = "MenuCode";
        public const string FIELD_RECORD_COUNT = "RecordCount";
        #endregion
        

        private String _connectionString;

        public MenuInRoleBO(string connString)
        {
            this._connectionString = connString;

        }
        public MenuInRoleBO()
        {
            this._connectionString = Common.ConnectionUtil.DHQG_ADMIN_CONNECTION_STRING;

        }

        public MenuInRoleDAO _MenuInRoleDAO;

        public MenuInRoleDAO CreateSQLDAO()
        {
            return new MenuInRoleDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<MenuInRoleModel> GetDynamicListPaging(MenuInRoleModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _MenuInRoleDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuInRoleModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {MenuInRoleModel.FIELD_MENU_IN_ROLE_ID,FIELD_MENU_IN_ROLE_ID},
                    {MenuInRoleModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {MenuInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION},  
                    {MenuInRoleModel.FIELD_MENU_NAME,FIELD_MENU_NAME},  
                    {MenuInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {MenuInRoleModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };
                return _MenuInRoleDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public MenuInRoleModel GetMenuInRoleById(int? id)
        {
            try
            {
                _MenuInRoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuInRoleModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {MenuInRoleModel.FIELD_MENU_IN_ROLE_ID,FIELD_MENU_IN_ROLE_ID},
                    {MenuInRoleModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {MenuInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {MenuInRoleModel.FIELD_MENU_NAME,FIELD_MENU_NAME}, 
                    {MenuInRoleModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION}, 
                    {MenuInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _MenuInRoleDAO.GetById(id, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<MenuInRoleModel> GetListMenuInRoleByRoleID(int? roleID, int? appID)
        {
            try
            {
                _MenuInRoleDAO = CreateSQLDAO();

                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {MenuInRoleModel.FIELD_APP_NAME,FIELD_APP_NAME},
                    {MenuInRoleModel.FIELD_MENU_IN_ROLE_ID,FIELD_MENU_IN_ROLE_ID},
                    {MenuInRoleModel.FIELD_MENU_ID,FIELD_MENU_ID},
                    {MenuInRoleModel.FIELD_ROLE_ID,FIELD_ROLE_ID},  
                    {MenuInRoleModel.FIELD_ROLE_NAME,FIELD_ROLE_NAME},  
                    {MenuInRoleModel.FIELD_MENU_NAME,FIELD_MENU_NAME}, 
                    {MenuInRoleModel.FIELD_MENU_CODE,FIELD_MENU_CODE},  
                    {MenuInRoleModel.FIELD_DESCRIPTION,FIELD_DESCRIPTION}, 
                    {MenuInRoleModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT}
                };

                return _MenuInRoleDAO.GetListMenuInRoleByRoleID(roleID, appID, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }


        public bool createMenuInRole(MenuInRoleModel model, int[] lstMenuID)
        {
            try
            {
                _MenuInRoleDAO = CreateSQLDAO();
                return _MenuInRoleDAO.createMenuInRole(model, lstMenuID);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteMenuInRole(int[] ID)
        {
            try
            {
                _MenuInRoleDAO = CreateSQLDAO();
                return _MenuInRoleDAO.deleteMenuInRole(ID);
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}